from django.core.management.base import BaseCommand
from products.models import ProductCategory

class Command(BaseCommand):
    help = '初始化产品分类数据'
    
    def handle(self, *args, **options):
        categories = [
            {'name': '股票', 'code': 'stock', 'description': '股票类投资产品'},
            {'name': '债券', 'code': 'bond', 'description': '债券类投资产品'},
            {'name': '基金', 'code': 'fund', 'description': '基金类投资产品'},
            {'name': '现金', 'code': 'cash', 'description': '现金类投资产品'},
            {'name': '货币基金', 'code': 'money_fund', 'description': '货币基金类投资产品'},
        ]
        
        created_count = 0
        for category_data in categories:
            category, created = ProductCategory.objects.get_or_create(
                code=category_data['code'],
                defaults={
                    'name': category_data['name'],
                    'description': category_data['description']
                }
            )
            if created:
                created_count += 1
                self.stdout.write(
                    self.style.SUCCESS(f'创建产品分类: {category.name} ({category.code})')
                )
            else:
                self.stdout.write(
                    self.style.WARNING(f'产品分类已存在: {category.name} ({category.code})')
                )
        
        self.stdout.write(
            self.style.SUCCESS(f'产品分类初始化完成，共创建 {created_count} 个新分类')
        )